package com.yjt.fresh.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.yjt.fresh.R;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.widget.CustomToastResult;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import butterknife.BindView;
import butterknife.ButterKnife;
import im.delight.android.webview.AdvancedWebView;

/**
 * 资讯详情
 * Created by mcc on 2018/7/14.
 */

public class AtyInfoDetail extends BaseSimpleActivity {
    private static final int REQUEST_PERMISSION_CODE = 1008;
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.ivAppAtyInfoDetailShare)
    ImageView mIvShare;
    @BindView(R.id.wbAppAtyInfoDetail)
    AdvancedWebView mWebView;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};
    @BindView(R.id.tvAppAtyInfoDetailTitle)
    TextView mTvATitle;
    @BindView(R.id.tvAppAtyInfoDetailTime)
    TextView mTvTime;
    @BindView(R.id.tvAppAtyInfoDetailAuthor)
    TextView mTvAuthor;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_info_detail;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        String title = getIntent().getStringExtra(MyConstants.TITLE);
        String time = getIntent().getStringExtra(MyConstants.TIME);
        String author = getIntent().getStringExtra(MyConstants.AUTHOR);
        mTvATitle.setText(title);
        mTvAuthor.setText(author);
        mTvTime.setText(time.split(" ")[0]);
    }

    @Override
    protected void initEvent() {
        setWebView();
        initClick();
    }

    private void setWebView() {
        String content = "";
        try {
            Document doc = Jsoup.parse(getIntent().getStringExtra(MyConstants.CONTENT));
            Elements elements = doc.getElementsByTag("img");
            for (Element element : elements) {
                element.attr("width", "100%").attr("height", "auto");
            }

            content = doc.toString();
        } catch (Exception e) {
            content = getIntent().getStringExtra(MyConstants.CONTENT);
        }
        mWebView.loadHtml(content);
    }

    private void initClick() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mIvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(mContext, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
                    } else {
                        share();
                    }
                } else {
                    share();
                }
            }
        });

    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_CODE) {
            for (int i = 0; i < permissions.length; i++) {
                Log.i("MainActivity", "申请的权限为：" + permissions[i] + ",申请结果：" + grantResults[i]);
            }
        }
    }

    private void share() {
        UMWeb umWeb = new UMWeb(getIntent().getStringExtra(MyConstants.INFO_DETAIL_H5));
        UMImage umImage = new UMImage(mContext, R.mipmap.ic_launcher);
        umWeb.setTitle("健康肉");
        umWeb.setDescription(mTvATitle.getText().toString());
        umWeb.setThumb(umImage);
        new ShareAction(mContext)
                .withMedia(umWeb)
                .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        CustomToastResult.show("分享成功");
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {

                        Toast.makeText(mContext, throwable.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {

                        Toast.makeText(mContext, "分享取消", Toast.LENGTH_SHORT).show();
                    }
                }).open();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(mContext).onActivityResult(requestCode, resultCode, data);
    }
}
